#define _CRT_SECURE_NO_WARNINGS 1
#include "Queue.h"
QueueNode* BuyNode(int index)
{
	QueueNode* buynode = (QueueNode*)malloc(sizeof(QueueNode));
	if (buynode == NULL)
	{
		perror("malloc error!\n");
		exit(1);
	}
	buynode->date = index;
	buynode->next = NULL;
	return buynode;
}
void QueueInit(Queue* pq)
{
	assert(pq);
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}

void QueuePush(Queue* pq, int index)
{
	assert(pq);
	QueueNode* newnode = BuyNode(index);
	if (pq->phead == NULL)
	{
		pq->phead = pq->ptail = newnode;
	}
	else
	{
		pq->ptail->next = newnode;
		pq->ptail = newnode;
	}
	++ pq->size;
}
void QueuePop(Queue* pq)
{
	assert(pq && pq->size);
	if (pq->phead == pq->ptail)
	{
		free(pq->phead);
		pq->phead = pq->ptail = NULL;
	}
	else
	{
		Queue* tmp = pq->phead;
		pq->phead = pq->phead->next;
	}
	--pq->size;
}
QDateType QueueFront(Queue* pq)
{
	assert(pq && pq->size);
	return pq->phead->date;
}
QDateType QueueBack(Queue* pq)
{
	assert(pq && pq->size);
	return pq->ptail->date;
}
bool QueueEmpty(Queue* pq)
{
	return !pq->size;
}
int QueueSize(Queue* pq)
{
	assert(pq);
	return pq->size;
}
void QueueDestroy(Queue* pq)
{
	assert(pq);
	QueueNode* tmp = pq->phead;
	while (tmp)
	{
		QueueNode* next = tmp->next;
		free(tmp);
		tmp = next;
	}
	pq->phead = pq->ptail = NULL;
	pq->size = 0;
}